package www.shw.com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import www.shw.com.bean.LvEx;
import www.shw.com.bean.Us;
import www.shw.com.service.LvService;

@Controller
public class LvController {

	@Resource
	private LvService lvService;
/**
 * 列表
 * @param request
 * @param page
 * @param rows
 * @param name
 * @return
 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request,
			@RequestParam(defaultValue="1",value="page")Integer page,
			@RequestParam(defaultValue="3",value="rows")Integer rows,
			String name
			) {
		Integer count = lvService.selectCount(name);
		Integer pages = count%rows==0?count/rows:count/rows+1;
		if (page>pages) {
			page=pages;
			if (page<1) {
				page=1;
			}
		}
		Integer startIndex = (page-1)*rows;
		List<LvEx> lvList = lvService.selectObjects(startIndex,rows,name);
		request.setAttribute("lvList", lvList);
		request.setAttribute("page", page);
		request.setAttribute("pages", pages);
		request.setAttribute("name", name);
		request.setAttribute("count", count);
		return "list";
	}
/**
 * 根据id查看
 * @param request
 * @param lv
 * @return
 */
	@RequestMapping("/look")
	public String look(HttpServletRequest request,LvEx lv) {
		lv = lvService.selectById(lv);
		request.setAttribute("lv", lv);
		return "look";
	}
/**
 * 去添加
 * @param request
 * @return
 */
	@RequestMapping("/toAdd")
	public String toAdd(HttpServletRequest request) {
		List<Us> usList = lvService.selectUs();
		request.setAttribute("usList", usList);
		return "add";
	}
/**
 * 去修改
 * @param request
 * @param lv
 * @return
 */
	@RequestMapping("/toUpdate")
	public String toUpdate(HttpServletRequest request,LvEx lv) {
		List<Us> usList = lvService.selectUs();
		lv = lvService.selectById(lv);
		request.setAttribute("usList", usList);
		request.setAttribute("lv", lv);
		return "add";
	}
/**
 * 编辑
 * @param request
 * @param lv
 * @return
 */
	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Boolean> add(HttpServletRequest request,LvEx lv) {
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		if (lv.getCid()!=null) {
			lvService.update(lv);
		} else {
			lvService.insert(lv);
		}
		map.put("flag", true);
		return map;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Boolean> delete(Integer[] ids) {
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		lvService.delete(ids);
		map.put("flag", true);
		return map;
	}
	
	
}
